layui.define(['jquery'], function (exports) {
    "use strict";

    /**
     * Gray Whale的功能
     * */
    var MOD_NAME = 'gray',
        $ = layui.jquery;

    var gray = new function () {

        this.open = function (options){
            //设置默认值
            options.area  = options.area ? options.area : ['100%', '100%'];
            options.type  = options.type ? options.type : 2;
            options.shade = options.shade ? options.shade : 0.1;
            layer.open(options);
        }

        this.param = function (name){
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) {
                return unescape(r[2]);
            }
            return null; //返回参数值
        }

        this.request = function (url, params, callback,timeoutSeconds,options) {
            $.apply()
            if(!timeoutSeconds){
                timeoutSeconds = 20000;//默认20秒
            }

            //支持URL参数
            // var contentType = "application/x-www-form-urlencoded";
            // var data = null;
            // if(params.url){
            //     contentType = "application/x-www-form-urlencoded";
            //     data = params.url;
            // }else{
            //     contentType = 'application/json;charset=UTF-8';
            //     data = JSON.stringify(params);
            // }

            let index = null;
            let ajaxOptions = {
                type: "POST",
                url: url,
                data: params,
                dataType: "json",
                contentType: 'application/json;charset=UTF-8',
                timeout: timeoutSeconds,
                traditional:true,
                beforeSend: function () {
                    index = layer.load("加载中...");
                },
                success: function (data) {
                    if (index) {
                        layer.close(index);
                    }
                    if (typeof callback == 'function') {
                        if(data.code == 200||data.success){
                            callback(data);
                        }else if(data.msg) {
                            layer.msg(data.msg, {icon: 2, time: 1000});
                            //layer.msg(data.msg);
                        }else {
                            layer.msg("服务器异常");
                        }

                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    debugger;
                    if (index) {
                        layer.close(index);
                    }

                    if("timeout" == textStatus){
                        layer.msg("请求超时，系统忙或网络异常");
                    }else if (XMLHttpRequest.responseText && XMLHttpRequest.responseText.indexOf("<!DOCTYPE html>") != -1) {
                        layer.confirm('登录超时，请重新登录', {
                            btn: ['确定', '取消'] //按钮
                        }, function () {
                            location.href = "/page/login-1.html";
                        });
                    } else {
                        switch (XMLHttpRequest.status) {
                            case(500):
                                layer.msg("服务异常");
                                break;
                            case(408):
                                layer.msg("请求超时");
                                break;
                            case(403):
                                layer.msg("无权限执行此操作");
                                break;
                            case(0):
                                layer.msg("网络连接异常");
                                break;
                            default:
                                layer.msg("请求失败，请联系管理人员");
                        }
                    }
                },
                complete: function (XHR, TS) {
                    XHR = null;
                }
            }

            //重写一些配置项
            if(options){
                for (let key in options) {
                    ajaxOptions[key] = options[key];
                }
            }
            return $.ajax(ajaxOptions);
        }

        this.post = function (url, params, callback,timeoutSeconds) {
            return this.request(url,params,callback,timeoutSeconds,{
                type:"POST",
                contentType: 'application/json;charset=UTF-8',
                data : JSON.stringify(params)
            });
        }

        this.put = function (url, params, callback,timeoutSeconds) {
            return this.request(url,params,callback,timeoutSeconds,{
                type:"PUT",
                contentType: 'application/json;charset=UTF-8',
                data : JSON.stringify(params)
            });
        }

        this.delete = function (url, params, callback,timeoutSeconds) {
            return this.request(url,params,callback,timeoutSeconds,{
                type:"DELETE",
                contentType: 'application/json;charset=UTF-8',
                data : JSON.stringify(params)
            });
        }

        this.get = function (url, params, callback,timeoutSeconds) {
            return this.request(url,params,callback,timeoutSeconds,{
                type:"GET",
                contentType: 'application/json;charset=UTF-8'
            });
        }




    }
    exports(MOD_NAME, gray);
});
